﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using Utility;
namespace Service
{
    /// <summary>
    /// 功能：HW_LoginLog类数据访问
    /// 创建人：Wilson
    /// 创建时间：2013-1-30   
    /// </summary>
    public class HW_LoginLogService : IDisposable
    {
        #region 公共
        private System.ComponentModel.Component component = new System.ComponentModel.Component();

        private bool disposed = false;

        ~HW_LoginLogService()
        {
            Dispose(false);
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {

                if (disposing)
                {

                    component.Dispose();
                }

                disposed = true;

            }
        }
        #endregion
		#region##MapperHW_LoginLog
        public IList<HW_LoginLog> MapperList(SqlDataReader reader)
         {
            IList<HW_LoginLog> list = new List<HW_LoginLog>();
            HW_LoginLog obj=null;
            while (reader.Read())
            {
                obj = new HW_LoginLog();
                obj.LoginOpId = (int)reader["LoginOpId"];
                obj.UserName = reader["UserName"] as string;
                obj.Name = reader["Name"] as string;
                obj.Ip = reader["Ip"] as string;
                obj.LoginTime = reader["LoginTime"] as DateTime?;
                list.Add(obj);
            }
            return list;
         }
        public HW_LoginLog Mapper(SqlDataReader reader)
         {
            HW_LoginLog obj=null;
            if (reader.Read())
            {
                obj = new HW_LoginLog();
                obj.LoginOpId = (int)reader["LoginOpId"];
                obj.UserName = reader["UserName"] as string;
                obj.Name = reader["Name"] as string;
                obj.Ip = reader["Ip"] as string;
                    obj.LoginTime = reader["LoginTime"] as DateTime?;
            }
            return obj;
         }
        #endregion
        #region##通过主键ID得到HW_LoginLog
        /// <summary>
        /// 功能：通过ID得到HW_LoginLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public HW_LoginLog GetById(int loginopid)
        {            
            string sql = "select * from HW_LoginLog where LoginOpId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            
            SqlDataReader reader=SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql,sp);
            return Mapper(reader);
        }
        #endregion
		
		#region##通过主键ID删除HW_LoginLog
        /// <summary>
        /// 功能：通过主键ID删除HW_LoginLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(int loginopid)
		{
			int i = 0;
			string sql = "delete from HW_LoginLog where LoginOpId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            i= SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
			return i;
		}
        /// <summary>
        /// 功能：通过主键ID删除HW_LoginLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(SqlTransaction transaction,int loginopid)
		{
			int i = 0;
			string sql = "delete from HW_LoginLog where LoginOpId = @PK";
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@PK",4)
            };
            i= SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
			return i;
		}
		#endregion			
		
        #region##添加HW_LoginLog
        /// <summary>
        /// 功能：添加HW_LoginLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(HW_LoginLog hw_loginlog)
        {
            int result = 0;
            string sql = "insert into HW_LoginLog(UserName,Name,Ip,LoginTime) values(@UserName,@Name,@Ip,@LoginTime)";			
            SqlParameter[] sp = new SqlParameter[]
            {
                //new SqlParameter("@LoginOpId",hw_loginlog.LoginOpId),
				new SqlParameter("@UserName",hw_loginlog.UserName),
				new SqlParameter("@Name",hw_loginlog.Name),
				new SqlParameter("@Ip",hw_loginlog.Ip),
				new SqlParameter("@LoginTime",hw_loginlog.LoginTime)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：添加HW_LoginLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(SqlTransaction transaction,HW_LoginLog hw_loginlog)
        {
            int result = 0;
            string sql = "insert into HW_LoginLog(LoginOpId,UserName,Name,Ip,LoginTime) values(@LoginOpId,@UserName,@Name,@Ip,@LoginTime)";			
            SqlParameter[] sp = new SqlParameter[]
            {
				new SqlParameter("@LoginOpId",hw_loginlog.LoginOpId),
				new SqlParameter("@UserName",hw_loginlog.UserName),
				new SqlParameter("@Name",hw_loginlog.Name),
				new SqlParameter("@Ip",hw_loginlog.Ip),
				new SqlParameter("@LoginTime",hw_loginlog.LoginTime)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##得到HW_LoginLog集合
		/// <summary>
        /// 功能：得到HW_LoginLog集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>       
        /// <returns></returns>
		public IList<HW_LoginLog> GetList()
		{
			string sql = "select * from HW_LoginLog";			
			SqlDataReader reader = SqlHelper.ExecuteReader(WebConn.connString,CommandType.Text,sql);
            return MapperList(reader);
		}
        /// <summary>
        /// 功能：得到HW_LoginLog集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>       
        /// <returns></returns>
        public DataTable GetList(int pageIndex, int pageSize, out int count)
        {
            string sql = "select top(@pageSize) * from HW_LoginLog where LoginOpId not in(select top(@pageSize*@pageIndex) LoginOpId from HW_LoginLog order by LoginOpId desc) order by LoginOpId desc";
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@pageSize",pageSize),
                new SqlParameter("@pageIndex",pageIndex),
            };
            DataTable data = SqlHelper.ExecuteDataset(WebConn.connString, CommandType.Text, sql,sp).Tables[0];
            string sql2 = "select count(*) from HW_LoginLog ";
            object o = SqlHelper.ExecuteScalar(WebConn.connString, CommandType.Text, sql2);
            count = Convert.ToInt32(o);
            return data;
        }
        public DataTable Search(int pageIndex, int pageSize, out int count, string searchString)
        {
            string sql = "select top(@pageSize) * from HW_LoginLog where (UserName like @searchString or Name like @searchString) and LoginOpId not in(select top(@pageSize*@pageIndex) LoginOpId from HW_LoginLog where (UserName like @searchString or Name like @searchString) order by LoginOpId desc) order by LoginOpId desc";
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@pageSize",pageSize),
                 new SqlParameter("@searchString","%"+searchString+"%"),
                new SqlParameter("@pageIndex",pageIndex),
            };
            DataTable data = SqlHelper.ExecuteDataset(WebConn.connString, CommandType.Text, sql, sp).Tables[0];
            string sql2 = "select count(*) from HW_LoginLog where (UserName like @searchString or Name like @searchString) ";
            SqlParameter[] sp2 = new SqlParameter[]{
                 new SqlParameter("@searchString","%"+searchString+"%"),
            };
            object o = SqlHelper.ExecuteScalar(WebConn.connString, CommandType.Text, sql2,sp2);
            count = Convert.ToInt32(o);
            return data;
        }
		#endregion
		
		#region##修改HW_LoginLog
        /// <summary>
        /// 功能：修改HW_LoginLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_LoginLog(HW_LoginLog hw_loginlog)
        {
            int result = 0;
            string sql = "update HW_LoginLog set LoginOpId = @LoginOpId,UserName = @UserName,Name = @Name,Ip = @Ip,LoginTime = @LoginTime where LoginOpId = @LoginOpId";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@LoginOpId",hw_loginlog.LoginOpId),
				new SqlParameter("@UserName",hw_loginlog.UserName),
				new SqlParameter("@Name",hw_loginlog.Name),
				new SqlParameter("@Ip",hw_loginlog.Ip),
				new SqlParameter("@LoginTime",hw_loginlog.LoginTime)
            };
            result = SqlHelper.ExecuteNonQuery(WebConn.connString,CommandType.Text,sql,sp);
            return result;
        }
         /// <summary>
        /// 功能：修改HW_LoginLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_LoginLog(SqlTransaction transaction,HW_LoginLog hw_loginlog)
        {
            int result = 0;
            string sql = "update HW_LoginLog set LoginOpId = @LoginOpId,UserName = @UserName,Name = @Name,Ip = @Ip,LoginTime = @LoginTime where LoginOpId = @LoginOpId";
            SqlParameter[] sp = new SqlParameter[]
            {       
				new SqlParameter("@LoginOpId",hw_loginlog.LoginOpId),
				new SqlParameter("@UserName",hw_loginlog.UserName),
				new SqlParameter("@Name",hw_loginlog.Name),
				new SqlParameter("@Ip",hw_loginlog.Ip),
				new SqlParameter("@LoginTime",hw_loginlog.LoginTime)
            };
            result = SqlHelper.ExecuteNonQuery(transaction,CommandType.Text,sql,sp);
            return result;
        }
        #endregion
		
		#region##统计HW_LoginLog
		/// <summary>
        /// 修改HW_LoginLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			int i = 0;
            string sql = "select count(*) from HW_LoginLog";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
		}
		/// <summary>
        /// 修改HW_LoginLog
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-30
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            int i=0;
            string sql="select count(LoginOpId) from HW_LoginLog";
            i = (int)SqlHelper.ExecuteScalar(WebConn.connString,CommandType.Text,sql);
            return i;
        }
		#endregion
        #region##自定义HW_LoginLog
        #endregion
                
    }
}
